<?php
LoadOnlineNow();

function LoadOnlineNow()
{
global $list,$dbsql,$ip;
	$list=array();
	$count=0;
	$timeold=time()-(60*50);
		
	$sql_select=" select * from tblonline where iTime>$timeold and iCountPage<1000 and iCountPage>0 order by iTime desc";
	$rs=$dbsql->query($sql_select);
	while($result = $dbsql->fetch_array($rs))
	{			
	   $country    = '';
		if(empty($result['cCountry']))
		{
			$country     = Ip2Country($result['cIp']);   
            $result['cCountry'] = $country;         			
		}
        if($result['cIp']==$ip)
        {
            $country    ='Admin';
        }
        if(!empty($country))
        {
            InsertCountry($result['cIp'],$country);
        }
		$list[$count]=$result;
		$count++;
	}
	$list['count']=$count;
	return $list;	
}

function InsertCountry($ip,$country)
{
global $language,$dbsql;
	$sql_update="update tblonline set cCountry='$country' where cIp='$ip'";
	$dbsql->query($sql_update);
}

function Ip2Country($ip)
{
	if(substr($ip,0,3)=="192" || substr($ip,0,3)=="127")
	{
		return "Local";
	}else
	if(substr($ip,0,3)=="123")
	{
		return "VietNam";
	}
	return file_get_contents('http://api.hostip.info/country.php?ip='.$ip);
}
?>